﻿using UnityEngine;
using System.Collections;

public class DaylightTime : MonoBehaviour
{
    float timeChange = 15.0f * 0.03f;
    public Material skyBoxMat;

    private GameObject character;
    private GameObject sunlight;

    private float materialBlendValue = 0.5f;

    void Start()
    {
        character = GameObject.Find("Player");
        sunlight = GameObject.Find("sunlight");
    }

    void Update()
    {
        transform.Rotate(0, timeChange * Time.deltaTime, 0);

        float materialAngleDifference = Vector3.Angle(character.transform.up, -sunlight.transform.forward);

        if (materialAngleDifference < 65)
        {
            materialBlendValue = Mathf.Lerp(materialBlendValue, 1.0f, Time.deltaTime * 0.5f);
            skyBoxMat.SetFloat("_Blend", materialBlendValue);
        }

        else if (materialAngleDifference >= 110)
        {
            materialBlendValue = Mathf.Lerp(materialBlendValue, 0.0f, Time.deltaTime * 0.5f);
            skyBoxMat.SetFloat("_Blend", materialBlendValue);
        }
        else
        {
            materialBlendValue = Mathf.Lerp(materialBlendValue, 0.5f, Time.deltaTime * 0.5f);
            skyBoxMat.SetFloat("_Blend", materialBlendValue);
        }
    }
}





